<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $yinhang=$_REQUEST['yinhang'];
        $zhihang=$_REQUEST['zhihang'];
        $kahao=$_REQUEST['kahao'];
        $xingming=$_REQUEST['xingming'];
        $yaotixian=$_REQUEST['yaotixian'];
		$sql="select * from `".$oto."_users` where `userId`='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_agentset` where `status`='1' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$agent_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$time=time();
		$ktx_yj=$user_info[0]['userMoney'];
		//$sqz_yj=$user_info[0]['agentWaitPrice'];
		$min=$agent_info[0]['minApplyPrice'];
		$max=$agent_info[0]['maxApplyPrice'];
		$loginName=$user_info[0]['userName'];
		$tel=$user_info[0]['userPhone'];
		$tx_yj=0;
		if($ktx_yj==0 || $yaotixian>$ktx_yj)
		{
			$data[0]['pan']=0;
			$data[0]['msg']='可提现余额不足';
			echo json_encode($data);
			exit();
		}
		else
		{
			if($min==0) ;
			else
			{
				if($yaotixian<$min)
				{
					$data[0]['pan']=0;
					$data[0]['msg']='提现金额未达到'.$min;
					echo json_encode($data);
					exit();
				}
			}
			$sql="select * from `".$oto."_withdraw` where `userId`='$user_id' and `status`='0' ";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$apply[]=$row;//将取得的所有数据赋值给person_info数组
			}
			if(isset($apply))
			{
				$data[0]['pan']=0;
				$data[0]['msg']="申请失败：已有申请审核中";
				echo json_encode($data);
				exit();
			}
			if($max==0) $tx_yj=$yaotixian;
			else
			{
				if($max<$yaotixian) $tx_yj=$max;
				else $tx_yj=$yaotixian;
			}
			

            $balance=$ktx_yj-$tx_yj;
            //$waitprice=$sqz_yj+$tx_yj;
            $sql="update ".$oto."_users set userMoney='{$balance}' where userId='{$user_id}'";
			$result=$db->query($sql);
			if($result)
			{
				// $sql="INSERT INTO ".$oto."_agent_apply (`userId`,`loginName`,`applyType`,`userType`,`tel`,`applyPrice`,`bankUserName`,`bankName`,`bankAccess`,`status`,`bankNum`,`time`) VALUES ('$user_id','$loginName','0','0','$tel','$tx_yj','$xingming','$yinhang','$zhihang','0','$kahao','$time')";
				$sql="select * from `".$oto."_withdraw` where bankNo='{$kahao}' and bankAddress='{$zhihang}' and bankId='{$yinhang}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$cy[]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(isset($cy)) $cy_pan=1;
				else $cy_pan=0;
				$txNo = date('YmdHis',time()).rand(10000,99999);
				$sql="INSERT INTO ".$oto."_withdraw (`userId`,`shopId`,`txNo`,`cardName`,`bankId`,`bankNo`,`bankAddress`,`isBnakDel`,`money`,`createTime`,`status`,`lastMoney`) VALUES ('$user_id','0','$txNo','$xingming','$yinhang','$kahao','$zhihang','$cy_pan','$tx_yj','$time','0','$balance')";
            	$result=$db->query($sql);
    //         	$sql="select * from `".$oto."_agent_apply` where `userId`='{$user_id}' order by `time` DESC";
				// $result=$db->query($sql);
				// while($row=$result->fetch_assoc()){
				// 	$ytx[]=$row;//将取得的所有数据赋值给person_info数组
				// }
				// $orderNo=$ytx[0]['id'];
				$orderNo=$txNo;
            	$data[0]['pan']=1;
				$sql="INSERT INTO ".$oto."_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`) VALUES ('$user_id','$orderNo','$tx_yj','$balance','$time','0','4','申请提现','3')";
	            $result=$db->query($sql);
	   //          $sql="select * from `".$oto."_apply_bank` where `userId`='{$user_id}' and bankNum='{$kahao}'";
				// $result=$db->query($sql);
				// while($row=$result->fetch_assoc()){
				// 	$cyyhk[]=$row;//将取得的所有数据赋值给person_info数组
				// }
				// if(!isset($cyyhk))
				// {
				// 	$sql="select * from `".$oto."_apply_bank` where `userId`='{$user_id}' and isDefault='1'";
				// 	$result=$db->query($sql);
				// 	while($row=$result->fetch_assoc()){
				// 		$cyyhk2[]=$row;//将取得的所有数据赋值给person_info数组
				// 	}
				// 	if(isset($cyyhk2))
				// 	{
				// 		$sql="update ".$oto."_apply_bank set isDefault='0' where userId='{$user_id}'";
				// 		$result=$db->query($sql);
				// 		$sql="INSERT INTO ".$oto."_apply_bank (`userId`,`bankUserName`,`bankName`,`bankNum`,`bankAccess`,`isDefault`,`time`) VALUES ('$user_id','$xingming','$yinhang','$kahao','$zhihang','1','$time')";
	   //          		$result=$db->query($sql);
				// 	}
				// 	else
				// 	{
				// 		$sql="INSERT INTO ".$oto."_apply_bank (`userId`,`bankUserName`,`bankName`,`bankNum`,`bankAccess`,`isDefault`,`time`) VALUES ('$user_id','$xingming','$yinhang','$kahao','$zhihang','1','$time')";
	   //          		$result=$db->query($sql);
				// 	}
					
				// }
			}
			else
			{
				$data[0]['pan']=0;
				$data[0]['msg']='提交失败';
			}
			
		}
		echo json_encode($data);
?>